home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Graphics⁄Sound
/
Macintosh Tracker Folder
/
Trecker Server Folder
/
Main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-30
|
6KB
|
282 lines
/* Main.c */
/* this file is based on "Simple Trecker.c" by Frank Seide */
#include <sound.h>
#include <GestaltEqu.h>
#include <Power.h>
#include "PSyn.h"
#include "STrI.h"
#include "mac_event.h"
#define CREATORCODE ('∫Tsq')
#define WAITNEXTEVENTDELAY (30)
extern Boolean ReceivedOpenEventFlag;
extern char FakeKeyBuffer[MAXKEYS];
extern int KeyBufPtr;
extern Boolean QuitPending;
/* parameters controlling the synthesis, with handy default values. */
extern short AntiAliasing;
extern short StereoOn;
extern unsigned short SamplingRate;
extern short NumRepeats;
extern short Speed;
extern short StereoMix;
extern short Loudness;
extern int RecalibratePlayer; /* set when settings change */
extern FSSpec GlobalFileSpec;
long PowerManagerInfo = 0;
struct PChannel* pc = NULL;
struct SoundTrack** strk = NULL;
Boolean Pausing = false;
void SetParameters(void);
void discard_buffer(void)
{
StopPChannel(pc, true); /* stop playing with fadeout */
UnlinkSoundTrack(strk); /* Unlink STrk from channel */
}
void HandleKeyBuffer(void)
{
WaitForEvent(15);
LoopPoint:
if (KeyBufPtr > 0)
{
int Scan;
int KeyTemp;
KeyTemp = (unsigned char)FakeKeyBuffer[0];
for (Scan = 1; Scan < KeyBufPtr; Scan += 1)
{
FakeKeyBuffer[Scan - 1] = FakeKeyBuffer[Scan];
}
KeyBufPtr -= 1;
switch (KeyTemp)
{
case '+':
Loudness += 8;
if (Loudness > 64)
{
Loudness = 64;
}
SetParameters();
UpdateSoundTrack(strk);
break;
case '-':
Loudness -= 8;
if (Loudness < 0)
{
Loudness = 0;
}
SetParameters();
UpdateSoundTrack(strk);
break;
case ' ':
TogglePause();
break;
case '>':
(**strk).musicRecord.fastForward = 3;
UpdateSoundTrack(strk);
break;
case '|':
(**strk).musicRecord.fastForward = 1;
UpdateSoundTrack(strk);
break;
case '<':
StopPChannel(pc,false);
ResetPChannel(pc);
StartPChannel(pc);
break;
}
goto LoopPoint;
}
}
void SetParameters(void)
{
(**strk).musicRecord.speedFactor = (100 * Speed) / 50;
(*pc).antiAlias = AntiAliasing;
StereoPChannel(pc,(Boolean)StereoOn);
if (NumRepeats == 0)
{
(**strk).musicRecord.loopDetect = FALSE;
}
else
{
(**strk).musicRecord.loopDetect = TRUE;
}
if (Loudness >= 64)
{
PChannelVolume(pc,-1,0x10000);
}
else
{
PChannelVolume(pc,-1,(0x10000 * (long)Loudness) / 64);
}
}
void main(void)
{
short i;
short WorkingDirectoryRefNum;
OSErr Error;
long ProcTypeInfo;
/* this may not be necessary, but I'll do it anyway to avoid problems */
InitGraf (&thePort);
InitFonts();
FlushEvents (everyEvent, 0);
InitWindows();
InitMenus();
TEInit();
InitDialogs (NULL);
InitCursor();
if (!RegisterEventHandlers())
{
return;
}
Error = Gestalt(gestaltPowerMgrAttr,&PowerManagerInfo);
if (Error != noErr)
{
return;
}
Error = Gestalt(gestaltProcessorType,&ProcTypeInfo);
if ((Error != noErr) || (ProcTypeInfo == gestalt68000))
{
FatalError(FatalError68020NeededID);
return;
}
if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
{
DisableIdle();
}
#ifdef AskForFile
{
Point Thing = {50,50};
StandardFileReply R;
/* this is only used for debugging when we don't have access to the interface */
/* program (since the interface program can't link to this program when it */
/* is being run under the debugger.) */
StandardGetFile(NULL,-1,NULL,&R);
GlobalFileSpec = R.sfFile;
}
#else
/* waiting for open document command to come */
while (!ReceivedOpenEventFlag && !QuitPending)
{
WaitForEvent(60);
}
if (QuitPending)
{
goto ExitPoint;
}
#endif
if (OpenPChannel (CHANNELS, true, 445*2, &pc))
{
FatalError(FatalErrorInternalError);
goto ExitPoint;
}
/* change to desired frequency */
pc->softFreq = (unsigned long)SamplingRate << 16;
pc->hardFreq = (unsigned long)SamplingRate << 16;
/* set maximum volume for 4 voices. */
for (i = 0; i < 4; i += 1)
{
PChannelVolume(pc,i,0x10000);
}
/* now loading the song. First, coerce the FSSpec into a working directory */
OpenWD(GlobalFileSpec.vRefNum,GlobalFileSpec.parID,
CREATORCODE,&WorkingDirectoryRefNum);
if (GetSoundTrack(WorkingDirectoryRefNum,GlobalFileSpec.name,0,&strk,FALSE))
{
CloseWD(WorkingDirectoryRefNum);
FatalError(FatalErrorNotASong);
goto ExitPoint;
}
CloseWD(WorkingDirectoryRefNum);
LinkSoundTrack (strk, pc);
SetParameters();
ResetPChannel (pc);
if (StartPChannel(pc))
{
FatalError(FatalErrorInternalError);
goto PreExitPoint;
}
/* now we do event loop waiting for codes & finish... */
while (!QuitPending)
{
if (RecalibratePlayer)
{
SetParameters();
UpdateSoundTrack(strk);
}
HandleKeyBuffer(); /* calls WaitForEvent */
/* is the soundtrack done? */
if ((**strk).musicRecord.nextOne)
{
QuitPending = true;
}
}
PreExitPoint:
ClosePChannel(pc);
ExitPoint:
if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
{
EnableIdle();
}
}
void TogglePause(void)
{
if (Pausing)
{
Pausing = false;
StartPChannel(pc);
}
else
{
Pausing = true;
StopPChannel(pc,false);
while (Pausing && !QuitPending)
{
HandleKeyBuffer();
}
}
}